package dev.ece.core.light;

import org.lwjgl.opengl.GL20;

import dev.ece.util.lib.Vector3f;
import dev.ece.util.lib.Vector4f;
/**
 * 材料
 * @author 飘渺青衣
 * <b>E-mail：</b>3375758@qq.com
 */
public class Material {
	
	private boolean enable = true;

	/**
	 * 散射光
	 */
	private Vector3f diffuse = new Vector3f();
	
	/**
	 * 高光元素
	 */
	private Vector3f specular = new Vector3f();
	
	private float shininess;
	/**
	 * 透明度
	 */
	private float alpha = 1;

	public void glUniformMaterial(MaterialLocationHandler handler) {
		GL20.glUniform1i(handler.getEnableHandler(), this.isEnable() ? 1 : 0);
		if(this.isEnable()) {
			diffuse.glUniform3f(handler.getDiffuseHandler());
			specular.glUniform3f(handler.getSpecularHandler());
			GL20.glUniform1f(handler.getShininessHandler(), shininess);
			GL20.glUniform1f(handler.getAlphaHandler(), alpha);
		}
	}
	
	public static String getMaterialStructString() {
		return "struct ece_Material {\n" +
				"bool enable;\n" +  
				"vec3 diffuse;\n" +  
				"vec3 specular;\n" +  
				"float shininess;\n" +  
				"float alpha;\n"+
			"};\n";
	}
	
	public void setDefaultMaterial() {
		getDiffuse().set(0.5f, 0.5f, 0.5f);
		getSpecular().set(0f, 0f, 0f);
		setShininess(32);
	}
	
	public float getShininess() {
		return shininess;
	}

	public void setShininess(float shininess) {
		this.shininess = shininess;
	}

	public void setDiffuse(Vector4f diffuse) {
		this.diffuse = diffuse;
	}

	public void setSpecular(Vector4f specular) {
		this.specular = specular;
	}

	public Vector3f getDiffuse() {
		return diffuse;
	}

	public Vector3f getSpecular() {
		return specular;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public float getAlpha() {
		return alpha;
	}

	public void setAlpha(float alpha) {
		this.alpha = alpha;
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder();
		s.append("Material(enable:");
		s.append(this.enable).append(",\nshininess:");
		s.append(this.shininess).append(",\nambient:");
		s.append(this.diffuse.toString()).append(",\nemission:");
		s.append(this.specular).append(")");
		return s.toString();
	}
	
}
